#!/bin/sh
#
# Switch between stable and preview repositories of mono.

STABLE_SOURCE="${STABLE_SOURCE:=http://ftp.novell.com/pub/mono/sources/}"
PREVIEW_SOURCE="${PREVIEW_SOURCE:=http://mono.ximian.com/monobuild/preview/sources/}"

case $1 in
	"stable")
		search="${PREVIEW_SOURCE}"
		replace="${STABLE_SOURCE}"
		;;
	"preview")
		search="${STABLE_SOURCE}"
		replace="${PREVIEW_SOURCE}"
		;;
	*)
		echo "usage: $0 stable|preview" >&2
		exit 1
		;;
esac

for Makefile in `grep -l "${search}" */*/Makefile`; do
	# Fix path
	cp ${Makefile} ${Makefile}.orig && sed -e "s|${search}|${replace}|" < ${Makefile}.orig > ${Makefile} && rm ${Makefile}.orig || exit 1
	# Remove fetched file
	portdir=`dirname ${Makefile}`
	distdir=`cd ${portdir} && make -V _DISTDIR`
	files=`cd ${portdir} && make -V DISTFILES`
	( cd ${distdir} && rm -f $files )
	# Fetch again
	( cd ${portdir} && port fetch )
done
